# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils autotools flag-o-matic versionator subversion

MY_PV=$(get_version_component_range $(get_version_component_count))
MY_PV=${MY_PV/p/}
ESVN_REPO_URI="https://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/linuxport/XBMC"
ESVN_REVISION=${MY_PV}

DESCRIPTION="XBMC is a free and open source media-player and entertainment hub"
HOMEPAGE="http://xbmc.org/"
#SRC_URI="mirror://sourceforge/xbmc/${P}.tar.gz"
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86 ~amd64"
IUSE="ccache gprof hal joystick mms opengl pcre xrandr"
RDEPEND="app-arch/bzip2
	app-arch/unrar
	app-arch/zip
	>=dev-lang/python-2.4
	dev-libs/boost
	dev-libs/fribidi
	pcre? ( dev-libs/libpcre )
	dev-libs/lzo
	dev-libs/tre
	=dev-python/pysqlite-2*
	ccache? ( dev-util/ccache )
	media-libs/alsa-lib
	media-libs/fontconfig
	media-libs/freetype
	media-libs/glew
	media-libs/jasper
	media-libs/libmad
	mms? ( media-libs/libmms )
	media-libs/libogg
	media-libs/libvorbis
	media-libs/sdl-gfx
	media-libs/sdl-image
	media-libs/sdl-mixer
	media-libs/sdl-sound
	net-misc/curl
	sys-apps/dbus
	hal? ( sys-apps/hal )
	sys-apps/pmount
	x11-libs/libXinerama
	xrandr? ( x11-libs/libXrandr )
	x11-libs/libXrender
	x11-proto/xineramaproto
	virtual/mysql
	dev-util/cmake
	opengl? ( virtual/opengl )"
DEPEND="${RDEPEND}"

pkg_setup() {
	if use joystick && built_with_use media-libs/libsdl nojoystick; then
		die "media-libs/libsdl needs to be built with USE -nojoystick."
	fi
}

src_unpack() {
	subversion_src_unpack

	#Fix Gentoo sanity failures, et al... is this just totally wack?
	epatch "${FILESDIR}/xmms-deprecated.patch"
	sed -e 's#^\(CPPLIBS_DIRS.*\)test_libFLAC++\(.*\)$#\1\2#g' \
		-i xbmc/cores/paplayer/flac-1.2.1/src/Makefile.am || die "sed failed..."
	sed -e 's#\(liba52/liba52/\)\(\*\.o\)#\1/.libs/\2#g' \
		-i xbmc/cores/dvdplayer/Codecs/Makefile* || die "sed failed..."
	#libDVDCSS barfs on all current *tex implementatinos, temporarily disable
	sed -e 's#^AC_PATH_PROG(LATEX.*$#LATEX="no"#g' \
		-i xbmc/cores/dvdplayer/Codecs/libDVDCSS/configure.ac || die "sed failed..."

	cd "${S}"
	eautoreconf
	for ltmain in `find . -name ltmain.sh ` ; do
		dir="`dirname ${ltmain}`"
		basedir="`basename ${dir}`"
		if		[[ -f "${dir}/configure.ac" || -f "${dir}/configure.in" ]] ; then
					cd "${dir}"
						eautoreconf
					cd "${S}"
		elif 	[[	-f "${dir}/../configure.ac" || \
					-f "${dir}/../configure.in" \
				]] && \
				[[	"${basedir}" == "autotools" || \
					"${basedir}" == ".auto" || \
					"${basedir}" == "config" \
				]] ; then
					cd "${dir}/.."
						eautoreconf
					cd "${S}"
		fi
	done
}

src_compile() {
	#-fomit-frame-pointer cannot be used with gprof
	if use gprof; then
		filter-flags "-fomit-frame-pointer"
	fi

	# force debug at the request of upstream until stable release
	econf \
		--prefix=/usr \
		$(use_enable ccache) \
		--enable-debug \
		$(use_enable gprof profiling) \
		$(use_enable hal halmount) \
		$(use_enable joystick) \
		$(use_enable mms) \
		$(use_enable opengl gl) \
		$(use_enable pcre) \
		$(use_enable xrandr) \
		|| die "Configure failed!"

	emake || die "Make failed!"

	einfo "Generating textures..."
	cd "${S}"
	for skin in skin/* ; do
		./tools/XBMCTex/XBMCTex -input "\"${skin}/media/\"" \
			-output "\"${skin}/media/Textures.xpr\"" || die "XBMCTex failed..."
	done

	einfo "Fixing case sensitivity..."
	mv "${S}/media/Fonts/arial.ttf" "${S}/media/Fonts/Arial.ttf"
	mv "${BUILDDIR}/media/Splash.png" "${BUILDDIR}/media/splash.png"

	einfo "Unzipping web content..."
	unzip "${S}"/web/Project_Mayhem_III_webserver_*.zip -d "${S}/web/" || die "Unzip web content failed..."
}

src_install() {
	einstall INSTALL_ROOT="${D}" || die "Install failed!"

	echo 'CONFIG_PROTECT="/usr/share/${PN}/userdata"' > "${D}/etc/env.d/95${PN}"

	for I in README.linux copying.txt LICENSE.GPL ; do
		dodoc "${I}"
	done
	for I in credits language media screensavers scripts skin sounds system userdata visualisations web ; do
		dodir "${I}"
	done
}
